package com.example.vae.Home.MySetting;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.example.vae.Activity.InformationActivity;
import com.example.vae.Activity.ScrollingActivity;
import com.example.vae.Activity.SheetActivity;
import com.example.vae.Adapter.FindRecyclerViewAdapter;
import com.example.vae.Adapter.SettingRecyclerViewAdapter;
import com.example.vae.MySettingViewModel;
import com.example.vae.Pojo.Person;
import com.example.vae.R;
import com.example.vae.Utils.ImageLoaderManager;
import com.example.vae.databinding.MySettingFragmentBinding;

import java.util.ArrayList;
import java.util.List;

public class MySettingFragment extends Fragment {

    private MySettingViewModel mViewModel;
    private MySettingFragmentBinding binding;


    private SettingRecyclerViewAdapter myRecyclerViewAdapter;
    private RecyclerView recyclerView;
    private List<Integer> imageviews=new ArrayList<>();
    private List<String> strings=new ArrayList<>();

    public static MySettingFragment newInstance() {
        return new MySettingFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        Log.d("MySettingFragment ","开始了");
        mViewModel = ViewModelProviders.of(requireActivity()).get(MySettingViewModel.class);

        binding = DataBindingUtil.inflate(inflater, R.layout.my_setting_fragment, container, false);
        binding.setData(mViewModel);
//        设置数据变化 的观察
        binding.setLifecycleOwner(requireActivity());

        mViewModel.setMySettingListener(new MySettingViewModel.MySettingListener() {
            @Override
            public void success(Person person) {
                Log.d("setMySettingListener",person.toString());
                ImageLoaderManager.loadImage(person.getPersonImgurl(),binding.touxiang);
            }
        });


        mViewModel.getLiveData().observe(this, new Observer<Person>() {
            @Override
            public void onChanged(Person person) {
//                加载头像
                Log.d("mViewModel","ing-------------");
                ImageLoaderManager.loadImage(mViewModel.getLiveData().getValue().getPersonImgurl(),binding.touxiang);
            }
        });




//       初始化数据
        inititem();

//        ImageLoaderManager.loadImage();

//        加载 mysetting 页面的 recycleview 数据
        myRecyclerViewAdapter=new SettingRecyclerViewAdapter(getContext(),imageviews,strings);
        recyclerView=binding.re;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(myRecyclerViewAdapter);


        binding.conFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getContext(), InformationActivity.class);
                startActivity(intent);
            }
        });

        binding.conFri.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getContext(), InformationActivity.class);
                startActivity(intent);
            }
        });

        binding.conGuanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getContext(), InformationActivity.class);
                startActivity(intent);
            }
        });
        return binding.getRoot();
    }

   public void inititem(){
        imageviews.add(R.drawable.ic_my_1);
       imageviews.add(R.drawable.ic_my_2);
       imageviews.add(R.drawable.ic_my_3);
       imageviews.add(R.drawable.ic_my_4);
       imageviews.add(R.drawable.ic_my_5);
       imageviews.add(R.drawable.ic_my_6);
       imageviews.add(R.drawable.ic_my_7);
       imageviews.add(R.drawable.ic_my_8);
       imageviews.add(R.drawable.ic_my_9);
       imageviews.add(R.drawable.ic_my_10);
       imageviews.add(R.drawable.ic_my_11);

       strings.add("松果账户");
       strings.add("会员中心");
       strings.add("订单管理");
       strings.add("邀请好友得礼");
       strings.add("勋章");
       strings.add("收藏");
       strings.add("荧光棒");
       strings.add("意见反馈");
       strings.add("帮助中心");
       strings.add("个人中心");
       strings.add("设置");

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        点击跳转到主页面
        binding.gerenzhuye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), SheetActivity.class);
                startActivity(intent);
            }
        });
        binding.go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), SheetActivity.class);
                startActivity(intent);
            }
        });


    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        Person personLiveData= mViewModel.getNowPerson();
//        Log.d("MySettingFragment ",personLiveData.toString());
//        if(personLiveData!=null){
//            Log.d("MySettingFragment的 img",personLiveData.toString());
//            ImageLoaderManager.loadImage(personLiveData.getPersonImgurl(),binding.touxiang);
//        }
//    }
}
